<?php

namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Session;
class Integral extends Controller{
    /****
     * Class register          	        积分商城商品
     * @$id[int]    		            分类id
     * @$integral_goods[array]        	分类查询
     * @$integral_goods_select[array]   全部商品查询
     * @date                	          2020/05/14 
     * @package application\index\controller\Integral
    */
    public function index(){
      $id = input('classify_id');
      if($id == 0){
           $integral_goods_select = Db::name('integral_goods')
                                    ->where(['status'=>1])
                                    ->order('sort desc')
                                    ->field('id,title,image,integral,money,original_price,inventory,has_change')
                                    ->paginate(6);
          if($integral_goods_select){
              $arr = [
                'state'                   => 1,
                'integral_goods_select'   =>$integral_goods_select,
                'message'                 => "所以产品！"
              ];
              return json($arr);
           }else{
              $arr = [
               'state'     => 0,
               'message'   => "暂无商品！"
              ];
              return json($arr);
           }
      }
     $integral_goods_select_1 = Db::name('integral_goods')->where(['status'=>1,'pid'=>$id])->select();
     if($integral_goods_select_1 == null){
          $arr = [
           'state'     => 0,
           'message'   => "暂无商品！"
          ];
        return json($arr);
     }
      $integral_goods_select = Db::name('integral_goods')
                  ->where(['status'=>1,'pid'=>$id])
                  ->order('sort desc')
                  ->field('id,title,image,integral,money,original_price,inventory,has_change')
                  ->paginate(6);
      if($integral_goods_select){
       		$arr = [
            'state'                   => 1,
            'integral_goods_select'          =>$integral_goods_select,
		    'message'                 => "分类产品！"
	       	];
        return json($arr);  
      }else{
       		$arr = [
		       'state'     => 0,
		       'message'   => "暂无商品！"
	       	];
        return json($arr);
      }
       
    }

    /****
     * Class register                   积分商城分类
     * @$integral[array]                查询
     * @date                            2020/05/14 
     * @package application\index\controller\Integral
    */
    public function classify(){
      $integral = Db::name('integral')
                  ->where(['status'=>1])
                  ->order('sort desc')
                  ->field('id,name')
                  ->select();
      if($integral){
           $arr = [
             'state'     => 1,
             'integral'  =>$integral,
             'message'   => "查询成功！"
           ];
       }else{
           $arr = [
             'state'     => 0,
             'message'   => "未注册请先注册！"
           ];
       }
      return json($arr);
    }

    
    /****
     * Class register                   积分商城详情页
     * @$integral_goods[array]                查询
     * @date                            2020/05/14 
     * @package application\index\controller\Integral
    */
    public function details(){
      $pid = input('pid');
      // $pid = 4;
      $integral_goods = Db::name('integral_goods')
                  ->where(['id'=>$pid,'status'=>1])
                  ->field('id,title,image,integral,money,original_price,editor,endtime')
                  ->find();
      if($integral_goods){
           $arr = [
             'state'     => 1,
             'integral_goods'  =>$integral_goods,
             'message'   => "查询成功！"
           ];
       }else{
           $arr = [
             'state'     => 0,
             'message'   => "该商品已下架！"
           ];
       }
      return json($arr);
    }
    /****
     * Class register                   积分商城订单页
     * @$order_number[int]                订单号
     * @$parameter[int]                 
     * @date                            2020/05/15
     * @package application\index\controller\Integral
    */
    
    public function integral_goods(){
      $parameter =input('post.');
      $amount = Db::name('integral_goods')->where(['id'=>$parameter['pid']])->find();
      // echo json_encode($amount);exit();
      if($amount['inventory'] < 1){
         $arr = [
             'state'     => 2,
             'message'   => "库存不足"
          ];
        return json($arr);
      }
      $order_number = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
      $data = [
        'integral'      =>$parameter['integral'],
		'pid'			 =>$parameter['pid'],
        'pound'         =>$parameter['pound'],
        'courier_id'    =>$parameter['courier_id'],
		'shipping_fee'	=>$parameter['shipping_fee'],
		'remark'		=>$parameter['remark'],
		'type_way'		=>$parameter['type_way'],
		'user_id'		=>$parameter['user_id'],
		'addtime'		=>time(),
        'order_number'  =>'ZTXY'.$order_number,
        'status'        =>1,
        'addtime'       =>time(),
      ]; 
      $integral_goods = Db::name('integral_order')->insert($data);
      if($integral_goods){
		  $integral_order = Db::name('integral_order')->where(['user_id'=>$parameter['user_id']])->order('id desc')->find(); 
        $arr = [
             'state'     => 1,
			 'integral_order'=>$integral_order,
             'message'   => "订单创建成功"
        ];
      }else{
        $arr = [
            'state'     => 0,
            'message'   => "订单创建失败"
        ];
      }
      return json($arr);

    }
	/****
     * Class register                  兑换记录
     * @date                            2020/05/15
     * @package application\index\controller\Integral
    */
    
	public function indent(){
		$user_id = input('user_id');
		$integral_order_select = Db::name('integral_order a')
						->join('integral_goods b','b.id = a.pid')
						->where(['a.user_id'=>$user_id])
						->select();
		if($integral_order_select == null){
			$arr = [
				 'state'     		  => 1,
				 'integral_order'     => '',
			];
			return json($arr);
		}
		$integral_order = Db::name('integral_order a')
						->join('integral_goods b','b.id = a.pid')
						->where(['a.user_id'=>$user_id])
						->paginate(15);
		 if($integral_order){
        $arr = [
             'state'     		  => 1,
			 'integral_order'     => $integral_order,
        ];
      }else{
        $arr = [
            'state'     		 => 1,
			'integral_order'     => '',
        ];
      }
      return json($arr);
	}
	
	public function integral_log(){
		$user_id = input('user_id');
		$integral_log_select = Db::name('integral_log')->where(['user_id'=>$user_id])->select();
		if($integral_log_select == null){
			$arr = [
				 'state'     		  => 1,
				 'integral_order'     => '',
			];
			return json($arr);
		}
		$integral_log = Db::name('integral_log')->where(['user_id'=>$user_id])->paginate(10);
		if($integral_log){
			$arr = [
				 'state'     		  => 1,
				 'integral_order'     => $integral_log,
			];
		}else{
			$arr = [
				'state'     		 => 1,
				'integral_order'     => '',
			];
		}
		return json($arr);
	}



}
